﻿/************************************************************************
 * SMLib - State Model Library
 * Copyright (C) 2012-2013 Ali Khalili (khalili_DOT_ir_@SIGN_gmail_DOT_com)
 * **********************************************************************
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License (LGPL) published 
 * by the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version. This program is distributed without any
 * warranty. See the GNU Lesser General Public License for more details. 
 * ***********************************************************************
 * File         :     SMLib\Interoperability\XML\XmlHelper.cs
 * Description  :     XML helper
 * Change log   :     -2011:  Initial Version (it was part of SimGine)
 * To Do        :     Test this component and fix potential bugs
 ************************************************************************/
using System;
using System.Collections.Generic;
using SMLib.Base;
using System.Text;
using System.IO;

namespace SMLib.Interoperability.XML
{
    /// <summary>
    /// Helper for Import from/Export to XML
    /// </summary>
    class XmlHelper
    	<StateType,TransitionType>
    {
        protected XmlHelper()
        {
            throw new Exception("I need a IExImHelper!");
        }

        IExImHelper<StateType,TransitionType> io;
        public XmlHelper(IExImHelper<StateType,TransitionType> io)
        {
            this.io = io;
        }
        

        /// <summary>
        /// Write to XML string
        /// </summary>
        /// <param name="ss">the state space</param>
        /// <returns>the XML string</returns>
        public string WriteToXML(IDiGraph<StateType,TransitionType> ss)
        {
            StringBuilder GMLString = new StringBuilder();
            int i = 0;
            GMLString.AppendLine("<graphml>");
            GMLString.AppendLine("\t<graph>");
            foreach (StateType st in ss.GetStates())
            {
                i++;
                GMLString.AppendLine("\t\t<node id=\"" + io.StringOf(st) + "\"/>");
                foreach (TransitionType tr in ss.TransitionsOf(st))
                {
                    GMLString.AppendLine("\t\t\t<edge id=\"" + io.StringOf(tr) + "\" source=\"" + io.StringOf(st) + "\" destination=\"" +
                	                     io.StringOf(ss.DestinationOf(tr)) + "\"/>");
                }
            }
            GMLString.AppendLine("\t</graph>");
            GMLString.AppendLine("\t</graphml>");
            return GMLString.ToString();
        }

        /// <summary>
        /// Write to XML file
        /// </summary>
        /// <param name="path">the absolute path of file</param>
        /// <param name="ss">the state space</param>
        public void WriteToXML(string path, IDiGraph<StateType,TransitionType> ss)
        {
            File.WriteAllText(path, WriteToXML(ss));
        }

        /// <summary>
        /// Read from XML file
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public virtual IDiGraph<StateType,TransitionType> ReadFromGml(string path)
        {
            //TranSys mySs = new TranSys();
            //XElement top = XElement.Load(path);
            //foreach (XElement node in top.Elements("node"))
            //{

            //}
            return null;
        }

        #region -=[ To Be Overrided ]=-

        /// <summary>
        /// Convert a vertex to string (for export porpuse)
        /// </summary>
        /// <param name="state">the state</param>
        /// <returns>the string</returns>
        protected virtual string VertexToVertexID(IStateContainer state)
        {
            return state.Identifier.ToString();
        }

        /// <summary>
        /// Converting an edge to string (for export porpuse)
        /// </summary>
        /// <param name="tr">the transition</param>
        /// <returns>the equivalent string</returns>
        protected virtual string EdgeToEdgeID(TransitionType tr)
        {
            return tr.ToString();
        }

        /// <summary>
        /// Convert a string into the proper transition (for import porpose)
        /// </summary>
        /// <param name="tr">the string</param>
        /// <returns>the equivalent transition</returns>
        protected virtual TransitionType EdgeIdToEge(string tr)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Convert a string into a state (for import porpose)
        /// </summary>
        /// <param name="st">the string</param>
        /// <returns>the equivalent state</returns>
        protected virtual IStateContainer VertexIdToVertex(string st)
        {
            throw new NotImplementedException();
        }
        #endregion

    }
}
